/**
 * @author zhangluping <13101501316@163.com>
 * @createTime 2018-09-07 15:38:46
 * @description
 */
import $http from '../utils/http.js'
import { Notification } from 'atelement'
// actions
const GET_ROLE_LIST = 'GET_ROLE_LIST'
const GET_ROLES_GROUP = 'GET_ROLES_GROUP'
const DELETE_ROLE = 'DELETE_ROLE'
const ADD_ROLE = 'ADD_ROLE'
const UPDATE_ROLES_GROUP = 'UPDATE_ROLES_GROUP'
const GET_COUNT = 'GET_COUNT'
const CLEAR_COUNT = 'CLEAR_COUNT'
const GET_SYNC_UPDATE = 'GET_SYNC_UPDATE'
const GET_SYNC_DELETE = 'GET_SYNC_DELETE'
const GET_SYNC_ADD = 'GET_SYNC_ADD'
const GET_HISTORY = 'GET_HISTORY'
// mutations
const SET_ROLE_LIST = 'SET_ROLE_LIST'
const SET_ROLES_GROUP = 'SET_ROLES_GROUP'
const SET_ROLE_ID = 'SET_ROLE_ID'
const SET_ROLE_NAME = 'SET_ROLE_NAME'
const SET_LOADING = 'SET_LOADING'
const SET_COUNT = 'SET_COUNT'
const SET_SYNC_TABLEDATA = 'SET_SYNC_TABLEDATA'
const SET_SYNC_ORDER = 'SET_SYNC_ORDER'
const SET_HIST_TABLEDATA = 'SET_HIST_TABLEDATA'
const SET_HIST_LOADING = 'SET_HIST_LOADING'
const SET_HIST_CURRENT = 'SET_HIST_CURRENT'
const SET_HIST_PAGESIZE = 'SET_HIST_PAGESIZE'
const SET_TAB_TYPE = 'SET_TAB_TYPE'
export default {
  namespaced: true,
  state: {
    loading: true,
    // 权限列表
    roles_group: [],
    // 角色id
    id: '',
    // 角色名
    name: '',
    // 角色表
    tableData: {
      total: 0,
      current: 1,
      dataSource: [],
      pageSize: 20
    },
    // 变更信息数
    count: 0,
    // 变更信息页
    sync: {
      order: {
        add: 'desc',
        delete: 'desc',
        update: 'desc'
      },
      tableData: {
        add: [],
        delete: [],
        update: []
      }
    },
    tabType: 'add',
    history: {
      loading: true,
      tableData: {
        total: 0,
        current: 1,
        dataSource: [],
        pageSize: 20
      }
    }
  },
  getters: {
    roles_group: state => state.roles_group,
    id: state => state.id,
    name: state => state.name,
    loading: state => state.loading,
    tableData: state => state.tableData,
    sync: state => state.sync,
    count: state => state.count,
    history: state => state.history
  },
  actions: {
    // 获取用户角色table表
    [GET_ROLE_LIST] ({ dispatch, commit, state }, value) {
      $http.option('/api/auth/roles', value).then(({ status, data } = {}) => {
        if (status === 'success') {
          commit(SET_ROLE_LIST, data)
        } else if (status === 'fail') {
          Notification.error({
            title: '获取用户角色信息失败'
          })
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        }
      })
    },
    // 获取角色权限列表
    [GET_ROLES_GROUP] ({ commit }, value) {
      commit('SET_LOADING', true)
      $http.post('/api/auth/roles/group', { role_id: value }).then(({ status, data } = {}) => {
        if (status === 'success') {
          commit(SET_ROLES_GROUP, data)
          commit('SET_LOADING', false)
        } else if (status === 'fail') {
          Notification.error({
            title: '获取用户角色权限列表失败'
          })
          commit('SET_LOADING', false)
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        }
      })
    },
    // 删除一个角色
    [DELETE_ROLE] ({ dispatch, commit }, value) {
      $http.post('/api/auth/roles/delete', { role_id: value.role_id }).then(({ status } = {}) => {
        if (status === 'success') {
          dispatch(GET_ROLE_LIST, value.baseSearch)
          Notification.success({
            title: '删除角色成功'
          })
        } else if (status === 'fail') {
          Notification.error({
            title: '删除角色失败'
          })
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        }
      })
    },
    // 添加角色
    [ADD_ROLE] ({ dispatch, commit }, value) {
      $http.post('/api/auth/roles/update', value.sendData).then(({ status, data } = {}) => {
        if (status === 'success') {
          commit(SET_ROLE_ID, data.role_id)
          commit(SET_ROLE_NAME, data.role_name)
          dispatch(GET_ROLES_GROUP, data.role_id)
          dispatch(GET_ROLE_LIST, value.baseSearch)
          Notification.success({
            title: '添加角色成功'
          })
        } else if (status === 'fail') {
          Notification.error({
            title: '添加角色失败'
          })
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        }
      })
    },
    // 更新角色
    [UPDATE_ROLES_GROUP] ({ dispatch, commit }, value) {
      $http.post('/api/auth/roles/update', value.sendData).then(({ status, data } = {}) => {
        if (status === 'success') {
          commit(SET_ROLE_ID, data.role_id)
          commit(SET_ROLE_NAME, data.role_name)
          dispatch(GET_ROLES_GROUP, data.role_id)
          dispatch(GET_ROLE_LIST, value.baseSearch)
          Notification.success({
            title: '修改成功'
          })
        } else if (status === 'fail') {
          Notification.error({
            title: '修改失败'
          })
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        }
      })
    },
    // 获取同步信息数
    [GET_COUNT] ({ commit }) {
      $http.post('/api/sync/role/num').then(({ status, data } = {}) => {
        if (status === 'success') {
          commit(SET_COUNT, data.num)
        } else if (status === 'fail') {
          Notification.error({
            title: '获取变更信息数失败'
          })
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        }
      })
    },
    // 查看同步信息后 清除计数
    [CLEAR_COUNT] ({ commit, dispatch }) {
      $http.post('/api/sync/role/clear').then(({ status, data } = {}) => {
        if (status === 'success') {
          commit(SET_COUNT, 0)
          dispatch(GET_SYNC_ADD)
          dispatch(GET_SYNC_UPDATE)
          dispatch(GET_SYNC_DELETE)
        } else if (status === 'fail') {
          Notification.error({
            title: '清空变更信息数失败'
          })
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        }
      })
    },
    // 获新增取同步信息
    [GET_SYNC_ADD] ({ commit, state }) {
      $http.post('/api/sync/role/list/add', { order: state.sync.order.add }).then(({ status, data } = {}) => {
        if (status === 'success') {
          commit(SET_SYNC_TABLEDATA, { type: 'add', data: data })
        } else if (status === 'fail') {
          Notification.error({
            title: '获取新增同步信息失败'
          })
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        }
      })
    },
    // 获取修改同步信息
    [GET_SYNC_UPDATE] ({ commit, state }) {
      $http.post('/api/sync/role/list/update', { order: state.sync.order.update }).then(({ status, data } = {}) => {
        if (status === 'success') {
          commit(SET_SYNC_TABLEDATA, { type: 'update', data: data })
        } else if (status === 'fail') {
          Notification.error({
            title: '获取修改同步信息失败'
          })
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        }
      })
    },
    // 获取删除同步信息
    [GET_SYNC_DELETE] ({ commit, state }) {
      $http.post('/api/sync/role/list/delete', { order: state.sync.order.delete }).then(({ status, data } = {}) => {
        if (status === 'success') {
          commit(SET_SYNC_TABLEDATA, { type: 'delete', data: data })
        } else if (status === 'fail') {
          Notification.error({
            title: '获取删除同步信息失败'
          })
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        }
      })
    },
    // 获取变更历史列表
    [GET_HISTORY] ({ commit, state }, value) {
      commit('SET_HIST_LOADING', true)
      $http.option('/api/sync/role/history/' + state.tabType, value).then(({ status, data } = {}) => {
        if (status === 'success') {
          commit(SET_HIST_TABLEDATA, data)
          commit('SET_HIST_LOADING', false)
        } else if (status === 'fail') {
          commit('SET_HIST_LOADING', false)
          Notification.error({
            title: '获取同步历史信息失败'
          })
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        }
      })
    }
  },
  mutations: {
    [SET_ROLE_LIST] (state, value) {
      state.tableData = value
    },
    [SET_ROLES_GROUP] (state, value) {
      state.roles_group = value
    },
    [SET_ROLE_ID] (state, value) {
      state.id = value
    },
    [SET_ROLE_NAME] (state, value) {
      state.name = value
    },
    [SET_LOADING] (state, value) {
      state.loading = value
    },
    [SET_COUNT] (state, value) {
      state.count = value
    },
    [SET_SYNC_TABLEDATA] (state, value) {
      state.sync.tableData[value.type] = value.data
    },
    [SET_SYNC_ORDER] (state, value) {
      state.sync.order[value.type] = value.data
    },
    [SET_HIST_TABLEDATA] (state, value) {
      state.history.tableData.dataSource = value.dataSource
      state.history.tableData.total = value.total
    },
    [SET_HIST_LOADING] (state, value) {
      state.history.loading = value
    },
    [SET_HIST_CURRENT] (state, value) {
      state.history.tableData.current = value
    },
    [SET_HIST_PAGESIZE] (state, value) {
      state.history.tableData.pageSize = value
    },
    [SET_TAB_TYPE] (state, value) {
      state.tabType = value
    }
  }
}
